package com.lanou.controller;

import com.lanou.bean.UserBean;
import com.lanou.service.LoginService;
import com.lanou.service.RegisterService;
import com.lanou.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import java.rmi.activation.UnknownObjectException;

/**
 * Created by dllo on 18/2/28.
 */
@Controller
public class UserController {
    @Resource
    private RegisterService registerService;
    @Resource
    private LoginService loginService;

    @RequestMapping("/homepage")
    public String pagea(){
        return "homepage";
    }




    @RequestMapping("/login")
    public String loginpage() {

        System.out.println("-------------------------------");
        if(SecurityUtils.getSubject().isAuthenticated()){
            return "redirect:homepage";
        }else{

            return "login_reg";
        }
    }

//    @RequestMapping("/loginsubmit")
//    public String loginsubmit(HttpServletRequest request) throws Exception {
//
//
//
//        //因为表单过滤器的原因，这个方法不需要接收用户名和密码，在进行Login
//        //而是由过滤器直接把用户名密码交给realm来做认证
//        //我们在这个方法中，就只处理异常就可以
//        //过滤器先于servlet执行，所以当在realm中处理认证时出现异常 过滤器会把异常信息保存到request中发给servlet，即交给我们来处理
//
//        //1.获取异常的名字
//        String exName = (String) request.getAttribute("shiroLoginFailure");
//
//        if(UnknownObjectException.class.getName().equals(exName)){
//            throw new UnknownAccountException("账号不存在");
//        }else if(IncorrectCredentialsException.class.getName().equals(exName)){
//            throw new IncorrectCredentialsException("密码错误");
//        }else{
//            throw new Exception("未知错误");
//        }
//
//
//    }

    @ResponseBody
    @RequestMapping("/loginsubmit")
    public AjaxResult loginsubmit(HttpServletRequest request) throws Exception {
        AjaxResult ajaxResult = new AjaxResult();
        String exName = (String) request.getAttribute("shiroLoginFailure");
        if (UnknownAccountException.class.getName().equals(exName)) {
//            throw new UnknownAccountException("账号不存在");
            ajaxResult.setCode("500");
            ajaxResult.setMsg("账号错误");
            ajaxResult.setData(exName);
            return ajaxResult;
        } else if (IncorrectCredentialsException.class.getName().equals(exName)) {
//            throw new IncorrectCredentialsException("密码错误");
            ajaxResult.setCode("500");
            ajaxResult.setMsg("密码错误");
            ajaxResult.setData(exName);
            return ajaxResult;
        } else {
//            throw new Exception("我也不知道为啥不对");
            ajaxResult.setCode("500");
            ajaxResult.setMsg("未知原因错误");
            ajaxResult.setData(exName);
            return ajaxResult;
        }

    }

    @ResponseBody
    @RequestMapping(value = "/nameState")
    public AjaxResult pageName(){
        AjaxResult result = new AjaxResult();

        Subject subject = SecurityUtils.getSubject();

        UserBean userBean = (UserBean) subject.getPrincipal();

        System.out.println(userBean+"123123213132123123123");
        result.setData(userBean);
        result.setMsg("存在用户");
        result.setCode("200");

        return result;

    }



    //@RequiresRoles("CO")
//    @RequestMapping("/test")
//    public String pageTest(){
//        if (SecurityUtils.getSubject().hasRole("CO")){
//            return "test";}
//        else {
//            return "redirect:home";
//        }
//    }




    @ResponseBody
    @RequestMapping(value = "/register" ,method = RequestMethod.POST)
    public AjaxResult pageReg(String username ,String password, String tel,String address){
        System.out.println(username+"~~~"+password+"~~~"+tel+"~~~"+address);
        AjaxResult result = registerService.userRegister(username,password,tel,address);
        return result;
    }



//    @ResponseBody
//    @RequestMapping(value = "/login",method = RequestMethod.POST )
//    public AjaxResult pageLogin(String username, String password, HttpServletRequest request){
//        AjaxResult result = loginService.login(username,password,request);
//        UserBean user= (UserBean) request.getSession().getAttribute("user");
//        System.out.println(user);
//        return result;
//    }




    @RequestMapping(value = "/statetohome")
    public String pageState(){
        return "homepage";
    }

    @RequestMapping(value = "/myblog")
    public String pageMyBlog(){
        return "myblog";
    }


}
